//index.js
var util = require('../../utils/util.js')
var app = getApp()
var router = require('../../utils/router.js')
var approuter = router.router
Page({
  data: {
    totalUrl:'',
    feed: [],
    feed_length: 0,
  },
  //跳转函数
  bindItemTap: function(event) {
    console.log(event.currentTarget);
    var avatarurl = event.currentTarget.dataset.avatarurl
    var subject = event.currentTarget.dataset.subject
    var intro = event.currentTarget.dataset.intro
    var teacher = event.currentTarget.dataset.teacher
    var address = event.currentTarget.dataset.address
    var startday = event.currentTarget.dataset.startday
    var starttime = event.currentTarget.dataset.starttime
    var endtime = event.currentTarget.dataset.endtime
    wx.navigateTo({
      url: '../answer/answer?avatarurl='+avatarurl+'&classsubject='+subject+'&classintro='+
      intro+'&teacher='+teacher+'&address='+address+'&startday='+startday+'&starttime='+starttime
      +'&endtime='+endtime
    })
  },
  bindQueTap: function() {
    wx.navigateTo({
      url: '../question/question'
    })
  },
  onLoad: function () {
    console.log('onLoad')
    this.setData({
      totalUrl: approuter.totalUrl
    })
    var that = this
    //调用应用实例的方法获取全局数据
    this.getData();
  },
  upper: function () {
    wx.showNavigationBarLoading()
    this.refresh();
    console.log("upper");
    setTimeout(function(){wx.hideNavigationBarLoading();wx.stopPullDownRefresh();}, 2000);
  },
  lower: function (e) {
    wx.showNavigationBarLoading();
    var that = this;
    setTimeout(function(){
      wx.hideNavigationBarLoading();
      that.nextLoad();
    }, 1000);
    console.log("lower")
  },
  onShow:function(){
    this.setData({
      totalUrl: approuter.totalUrl
    })
    var that = this
    //调用应用实例的方法获取全局数据
    this.getData();
  },
  //使用本地 fake 数据实现刷新效果
  getData: function(){
    var that = this
    // var feed = util.getData2(); // 本地获取数据
    var header = { 'content-type': 'application/json'}
    var data = {}
    util.sendRequest(that.data.totalUrl, 'GET', data, header)
      .then(function (reponse) {
        // 成功调用
        // 成功调用
        if (reponse.data.Status == 'SUCCESS') {
          that.setData({
            // 设置值
            feed: reponse.data.Data,
            feed_length: reponse.data.Data.length
          })
        }
        console.log(reponse)
      }, function (error) {
        // 失败调用
        console.log(error)
      })
  },
  refresh: function(){
    wx.showToast({
      title: '刷新中',
      icon: 'loading',
      duration: 3000
    });
    this.getData();
    setTimeout(function(){
      wx.showToast({
        title: '刷新成功',
        icon: 'success',
        duration: 2000
      })
    },3000)
  },

  //使用本地 fake 数据实现继续加载效果
  nextLoad: function(){
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 4000
    })
    this.getData();
    setTimeout(function(){
      wx.showToast({
        title: '加载成功',
        icon: 'success',
        duration: 2000
      })
    },3000)
  }
})
